package me.pplay.playtalk.activity.friends;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.HomeActivity;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.view.MyImageView;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;


public class FriendsMapActivity extends TabActivity{

	private RadioGroup group;
	private TabHost tabHost;
	private ImageButton backBtn, searchBtn;
	private MyImageView mImageView;
	private HorizontalScrollView mHorizontalScrollView;
	
	private int currentPosition = 0;
	private int sdkVersion      = 0;
	private int mCurrentCheckedRadioLeft = 0;
	private String[] mTotalColum;
	
	
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		appState.addActivityToContainer(this);
		setContentView(R.layout.layout_friends_map);
		findViews();
		initDatas();
		clickEvents();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		appState.removeActivityToContainer(this);
		super.onDestroy();
	}

	protected void findViews() {
		backBtn			= (ImageButton)findViewById(R.id.backBtn);
		searchBtn		= (ImageButton)findViewById(R.id.searchBtn);
		group 			= (RadioGroup)findViewById(R.id.radioGroup);
		mImageView		= (MyImageView)findViewById(R.id.img1);
		mHorizontalScrollView =(HorizontalScrollView)findViewById(R.id.horizontalScrollView);
		tabHost 		= getTabHost();
	}
   
	protected void initDatas() {
		mTotalColum     = getResources().getStringArray(R.array.array_friends_map);
		iniController();
		sdkVersion		= Build.VERSION.SDK_INT;
		tabHost.addTab(tabHost.newTabSpec(mTotalColum[0]).setIndicator(mTotalColum[0]).setContent(new Intent(FriendsMapActivity.this,FriendsActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(mTotalColum[1]).setIndicator(mTotalColum[1]).setContent(new Intent(FriendsMapActivity.this,DiscoverActivity.class)));
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case 0:
					tabHost.setCurrentTabByTag(mTotalColum[0]);
					break;
				case 1:
					searchBtn.setVisibility(View.GONE);
					tabHost.setCurrentTabByTag(mTotalColum[1]);
					break;
				}
				AnimationSet _AnimationSet = new AnimationSet(true);
				TranslateAnimation _TranslateAnimation;
				_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, (float)(checkedId*(Globals.width/3)), 0f, 0f);
				_AnimationSet.addAnimation(_TranslateAnimation);
				_AnimationSet.setFillBefore(true);
				_AnimationSet.setFillAfter(true);
				_AnimationSet.setDuration(200);
				mImageView.startAnimation(_AnimationSet);
				mCurrentCheckedRadioLeft = (group.getCheckedRadioButtonId())*(Globals.width/3);
				mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)(Globals.width/3), 0); 
			}
		});
	}
	
	private void iniController(){
		int count 		= mTotalColum.length;
		for(int i = 0; i < count; i++){
			RadioButton rdo = new RadioButton(this);
			rdo.setText(mTotalColum[i]);
			rdo.setId(i);
			setRadioGroupStyle(rdo);
			group.addView(rdo);
		}
	}
	
	private void clickEvents(){
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FriendsMapActivity.this, HomeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				if (sdkVersion >= 5)
					overridePendingTransition(R.anim.right_in, R.anim.noanim);
			}
		});
		
		searchBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(currentPosition == 0){
					Intent intent  = new Intent(FriendsMapActivity.this, FriendsActivity.class);
					intent.putExtra("isSearch", true);
					startActivity(intent);
				}
			}
		});
	}
	
	private void setRadioGroupStyle(RadioButton rdo){
		 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Globals.width/3, LayoutParams.FILL_PARENT);
		 params.weight	= (float)1.0;
		 rdo.setLayoutParams(params);
		 rdo.setPadding(10, 10, 10, 10);
		 rdo.setGravity(Gravity.CENTER);
		 rdo.setButtonDrawable(null);
		 rdo.setEllipsize(TruncateAt.MARQUEE);
		 rdo.setSingleLine(true);
		 rdo.setBackgroundResource(R.drawable.radiobtn_selector);
	}
	
}
